import { defineStore } from 'pinia'
import api from '@/api/request'

export const useMetroStore = defineStore('metro', {
  state: () => ({
    lines: [], // 地铁线路列表
    stations: {}, // 各线路站点
    choosedLine: null, // 当前选中线路
    arrivalInfo: {}, // 到站信息
    loading: false,
    error: null,
  }),
  actions: {
    async fetchData() {
      this.loading = true
      this.error = null
      try {
        const data = await api.get('/api/lines')
        this.lines = data.lines || []
        this.stations = data.stations || {}
      } catch (error) {
        this.error = error.message || 'Failed to fetch metro data'
        console.error('Fetch metro data error:', error)
      } finally {
        this.loading = false
      }
    },
    updateArrivalInfo(station, time) {
      this.arrivalInfo[station] = time
    },
  },
})
